package com.cardone.platform.usercenter.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.usercenter.dto.UsDepartmentTranslateDto;

/**
 * 用户部门调动
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class UsDepartmentTranslateJdbcDao implements UsDepartmentTranslateDao {
	@Override
	public int deleteByIds(final UsDepartmentTranslateDto deleteUsDepartmentTranslate) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteUsDepartmentTranslate.getAttrs())) {
			model.putAll(deleteUsDepartmentTranslate.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteUsDepartmentTranslate.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final UsDepartmentTranslateDto findUsDepartmentTranslate) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsDepartmentTranslate, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findUsDepartmentTranslate, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findUsDepartmentTranslate.getAttrs())) {
			model.putAll(findUsDepartmentTranslate.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final UsDepartmentTranslateDto findUsDepartmentTranslate) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsDepartmentTranslate);

		final Model model = ModelUtils.newModel(new ModelArgs(findUsDepartmentTranslate, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findUsDepartmentTranslate.getAttrs())) {
			model.putAll(findUsDepartmentTranslate.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final UsDepartmentTranslateDto findListUsDepartmentTranslate) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListUsDepartmentTranslate).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListUsDepartmentTranslate.getAttrs())) {
			model.putAll(findListUsDepartmentTranslate.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final UsDepartmentTranslateDto insertUsDepartmentTranslate) {
		if (StringUtils.isBlank(insertUsDepartmentTranslate.getId())) {
			insertUsDepartmentTranslate.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertUsDepartmentTranslate));

		if (MapUtils.isNotEmpty(insertUsDepartmentTranslate.getAttrs())) {
			model.putAll(insertUsDepartmentTranslate.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final UsDepartmentTranslateDto paginationUsDepartmentTranslate) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationUsDepartmentTranslate).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationUsDepartmentTranslate.getAttrs())) {
			model.putAll(paginationUsDepartmentTranslate.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationUsDepartmentTranslate), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final UsDepartmentTranslateDto readUsDepartmentTranslate) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readUsDepartmentTranslate, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readUsDepartmentTranslate.getAttrs())) {
			model.putAll(readUsDepartmentTranslate.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readUsDepartmentTranslate);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readUsDepartmentTranslate, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final UsDepartmentTranslateDto saveUsDepartmentTranslate) {
		final P oldUsDepartmentTranslate = this.findByCodeOrId(mappedClass, saveUsDepartmentTranslate);

		if (oldUsDepartmentTranslate == null) {
			this.insertByCode(saveUsDepartmentTranslate);

			return this.findByCodeOrId(mappedClass, saveUsDepartmentTranslate);
		}

		final int updateCount = this.updateByCode(saveUsDepartmentTranslate, oldUsDepartmentTranslate);

		if (updateCount < 1) {
			return oldUsDepartmentTranslate;
		}

		return this.findByCodeOrId(mappedClass, saveUsDepartmentTranslate);
	}

	@Override
	public int updateByCode(final UsDepartmentTranslateDto updateUsDepartmentTranslate) {
		final UsDepartmentTranslateDto oldUsDepartmentTranslate = this.findByCodeOrId(UsDepartmentTranslateDto.class, updateUsDepartmentTranslate);

		return this.updateByCode(updateUsDepartmentTranslate, oldUsDepartmentTranslate);
	}

	private <P> int updateByCode(final UsDepartmentTranslateDto updateUsDepartmentTranslate, final P oldUsDepartmentTranslate) {
		if (oldUsDepartmentTranslate == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateUsDepartmentTranslate);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateUsDepartmentTranslate, oldUsDepartmentTranslate, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldUsDepartmentTranslate, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldUsDepartmentTranslate, updateUsDepartmentTranslate, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateUsDepartmentTranslate, updateProperties), new ModelArgs(updateUsDepartmentTranslate, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateUsDepartmentTranslate.getAttrs())) {
			model.putAll(updateUsDepartmentTranslate.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}